package com.hivision.controller;


import com.google.gson.annotations.Since;
import com.hivision.alimqttapi.util.AttributeValue;
import com.hivision.iot.api.AliReqClient;
import com.hivision.iot.dto.AttributeValueRequestDTO;
import com.hivision.mapper.PassRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.chrono.ChronoLocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author 温嘉明
 * 设备状态
 */
@RequestMapping("/EquipmentStatus")
@RestController
public class EquipmentStatus {
    @Autowired
    private AliReqClient reqClient;
    @Autowired
    AttributeValue attributeValue;

    List<String> equipments = Arrays.asList("DN_PS", "DN_PTED", "DN_PVC1", "DN_TC1", "DN_TC2", "DN_WS", "DN_Public","DN_Con");

    @Scheduled(cron = "0 */1 * * * ?")
    @GetMapping("/updateStatus")
    public void updateStatus() {
        // 获取当前时间
        LocalTime currentTime = LocalTime.now();
        // 创建一个 DateTimeFormatter 对象用于格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
        // 格式化当前时间为字符串
        String formattedTime = currentTime.format(formatter);
        LocalTime checkDate = LocalTime.parse(formattedTime, formatter);

        LocalTime startTime1 = LocalTime.parse("07:50", formatter);
        LocalTime startTime2 = LocalTime.parse("07:55", formatter);
        LocalTime startTime3 = LocalTime.parse("16:00", formatter);
        LocalTime startTime4 = LocalTime.parse("16:05", formatter);
        LocalTime startTime6 = LocalTime.parse("19:50", formatter);
        LocalTime startTime7 = LocalTime.parse("19:55", formatter);
        LocalTime startTime8 = LocalTime.parse("04:00", formatter);
        LocalTime startTime9 = LocalTime.parse("04:05", formatter);
        if (isbetween(startTime1, startTime2) || isbetween(startTime6, startTime7)) {
            for (String equipment : equipments) {
                if(equipment.equals("DN_PTED") && !isOne() ){
                    continue;
                }
                AttributeValueRequestDTO attributeValueRequestDTO = new AttributeValueRequestDTO();
                attributeValueRequestDTO.setAssetCode(equipment);
                attributeValueRequestDTO.setAttributeCode("OpenLine");
                attributeValueRequestDTO.setValue("1");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
                attributeValueRequestDTO.setAttributeCode("TemRise");
                attributeValueRequestDTO.setValue("1");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
                attributeValueRequestDTO.setAttributeCode("Produce");
                attributeValueRequestDTO.setValue("1");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
                attributeValueRequestDTO.setAttributeCode("LineStop");
                attributeValueRequestDTO.setValue("0");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
                attributeValueRequestDTO.setAttributeCode("Shutdown");
                attributeValueRequestDTO.setValue("0");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
            }
        }
        if (isbetween(startTime3, startTime4) || isbetween(startTime8, startTime9)) {
            for (String equipment : equipments) {
                if(equipment.equals("DN_PTED") && !isFive() ){
                    continue;
                }
                AttributeValueRequestDTO attributeValueRequestDTO = new AttributeValueRequestDTO();
                attributeValueRequestDTO.setAssetCode(equipment);
                attributeValueRequestDTO.setAttributeCode("LineStop");
                attributeValueRequestDTO.setValue("1");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
                attributeValueRequestDTO.setAttributeCode("Shutdown");
                attributeValueRequestDTO.setValue("1");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
                attributeValueRequestDTO.setAssetCode(equipment);
                attributeValueRequestDTO.setAttributeCode("OpenLine");
                attributeValueRequestDTO.setValue("0");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
                attributeValueRequestDTO.setAttributeCode("TemRise");
                attributeValueRequestDTO.setValue("0");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
                attributeValueRequestDTO.setAttributeCode("Produce");
                attributeValueRequestDTO.setValue("0");
                reqClient.patchAttributeValue(attributeValueRequestDTO);
            }

        }

    }



    public Boolean isbetween(LocalTime startTime, LocalTime endTime) {
        // 获取当前时间
        LocalTime currentTime = LocalTime.now();
        // 创建一个 DateTimeFormatter 对象用于格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
        // 格式化当前时间为字符串
        String formattedTime = currentTime.format(formatter);
        LocalTime checkDate = LocalTime.parse(formattedTime, formatter);
        boolean isBetween = !checkDate.isBefore(startTime) && !checkDate.isAfter(endTime);
        return isBetween;
    }

    public Boolean isOne(){
        LocalDate today = LocalDate.now();
        DayOfWeek dayOfWeek = today.getDayOfWeek();
        if (dayOfWeek == DayOfWeek.MONDAY) {
            return true;
        }
        return false;
    }


    public Boolean isFive(){
        LocalDate today = LocalDate.now();
        DayOfWeek dayOfWeek = today.getDayOfWeek();
        if (dayOfWeek == DayOfWeek.MONDAY) {
            return true;
        }
        return false;
    }

}
